package de.alex.schulzeit.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class Subjecttable {

	private DatabaseHelper database;
	private static final String TAG = Subjecttable.class.getSimpleName();
	
	public Subjecttable(DatabaseHelper db) {
		database = db;
	}
	
	public Cursor getAllTimeTableSubjects() {
		Cursor result = null;
		SQLiteDatabase test = null;
		try {
			test = database.getReadableDatabase();
		} catch (Exception e) {
			Log.d(TAG, "Fehler get readable");
		}
		try {
			result = test.rawQuery(
				"SELECT tst._id, tst.subjectID, tst.teacherID, tst.day, tst.hour, st.subject, tt.name " +
				"FROM TableSubjectTable AS tst " + 
				"LEFT OUTER JOIN SubjectTable AS st ON tst.subjectID = st._id " + 
				"LEFT OUTER JOIN TeacherTable AS tt ON tst.teacherID = tt._id " + 
				"ORDER BY day ASC, hour ASC;", null);
			
		} catch (Exception e) {
			Log.d(TAG, e.getMessage());
		} 
		return result;	
	}
	
	public Cursor getTimeTableSubjectsForDay(int day) {
		Cursor result = null;
		SQLiteDatabase test = null;
		try {
			test = database.getReadableDatabase();
		} catch (Exception e) {
			Log.d(TAG, "Fehler get readable");
		}
		try {
			result = test.rawQuery(
				"SELECT tst._id, tst.subjectID, tst.teacherID, tst.day, tst.hour, st.subject, tt.name " +
				"FROM TableSubjectTable AS tst " + 
				"LEFT OUTER JOIN SubjectTable AS st ON tst.subjectID = st._id " + 
				"LEFT OUTER JOIN TeacherTable AS tt ON tst.teacherID = tt._id " + 
				"WHERE tst.day = '" + day + "'" +
				"ORDER BY day ASC, hour ASC;", null);
			
		} catch (Exception e) {
			Log.d(TAG, e.getMessage());
		} 
		return result;
	}
}
